package main

import (
	"encoding/binary"
	"log"
	"net"
)

func main() {
	listener, err := net.Listen("tcp", ":8080")
	if err != nil {
		log.Fatal(err)
	}

	for {
		conn, err := listener.Accept()
		if err != nil {
			log.Fatal(err)
		}
		go handleConn(conn)
	}

}

func handleConn(conn net.Conn) {
	intchan := make(chan int, 1024)

	go func() {
		for i := 0; i < 10000; i++ {
			intchan <- i
		}
	}()

	go func() {
		for i := 10000; i < 20000; i++ {
			intchan <- i
		}
	}()

	for {
		val := <-intchan
		bytes := make([]byte, 8)
		binary.BigEndian.PutUint64(bytes, uint64(val))

		writeLen, err := conn.Write(bytes)
		if err != nil {
			log.Println(err)
		}

		log.Printf("send %d\n", writeLen)
	}

}
